#include "asr_stream.h"
#include "port_audio.h"

#include "conf.h"

#include <memory>
#include <signal.h>
#include <unistd.h>
#include <string>

#include <stdlib.h>

namespace {
    std::unique_ptr<AsrStream>  asr;

    AsrStream::asrContinueEnum asr_callback(std::string &res)
    {
        sys_log("%s", res.c_str());

        // asr->pause();

        return AsrStream::asrContinue;
    }

    void signalHandler(int signum) 
    {
        sys_log("signal handler .... exit ......");
        if (asr)    asr->stop();
        asr.reset();
        exit(signum);  // 释放资源
    }
}

int main(void)
{
    signal(SIGINT, signalHandler);

    system("/home/mdical/llm_node/src/llm_node/scripts/setDefaultInput.sh");

    // 初始化全局的 portAudio 库
    std::unique_ptr<PortAudioManager> pam = std::make_unique<PortAudioManager>();

    // 初始化语音识别类
    asr = std::make_unique<AsrStream>(
        "/opt/sherpa-onnx/sherpa-onnx-streaming-zipformer-bilingual-zh-en"
    );


    asr->setAsrCallback(asr_callback);

    asr->resume();

#if 0
// 测试语音的停止和恢复
    sleep(5);

    sys_log("stop asr audio stream.");
    asr->stop();

    sleep(5);

    sys_log("start asr audio stream.");
    asr->start();

    sleep(5);

    sys_log("stop asr audio stream.");
    asr->stop();

    sleep(5);
#endif 

    // 10 minute asr input test.
    sleep(60 * 10);

    exit(0);
}
